﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Farmer
    {
        public bool CreateOneFarmer(Farmer_Info objFarmer_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Farmer_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FarmID", objFarmer_Info.FarmID));
            command.Parameters.Add(new SqlParameter(@"MAFFLicenceID", objFarmer_Info.MAFFLicenceID));
            command.Parameters.Add(new SqlParameter(@"FarmerName", objFarmer_Info.FarmerName));
            command.Parameters.Add(new SqlParameter(@"FarmName", objFarmer_Info.FarmName));
            command.Parameters.Add(new SqlParameter(@"Username", objFarmer_Info.Username));
            command.Parameters.Add(new SqlParameter(@"Password", objFarmer_Info.Password));
            command.Parameters.Add(new SqlParameter(@"Address", objFarmer_Info.Address));
            command.Parameters.Add(new SqlParameter(@"PhoneNumber", objFarmer_Info.PhoneNumber));
            command.Parameters.Add(new SqlParameter(@"Email", objFarmer_Info.Email));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneFarmer(Farmer_Info objFarmer_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Farmer_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FarmID", objFarmer_Info.FarmID));
            command.Parameters.Add(new SqlParameter("@MAFFLicenceID", objFarmer_Info.MAFFLicenceID));
            command.Parameters.Add(new SqlParameter("@FarmerName", objFarmer_Info.FarmerName));
            command.Parameters.Add(new SqlParameter("@FarmName", objFarmer_Info.FarmName));
            command.Parameters.Add(new SqlParameter("@Username", objFarmer_Info.Username));
            command.Parameters.Add(new SqlParameter("@Password", objFarmer_Info.Password));
            command.Parameters.Add(new SqlParameter("@Address", objFarmer_Info.Address));
            command.Parameters.Add(new SqlParameter("@PhoneNumber", objFarmer_Info.PhoneNumber));
            command.Parameters.Add(new SqlParameter("@Email", objFarmer_Info.Email));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneFarmer(int FarmID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Farmer_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FarmID", FarmID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllFarmer()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Farmer_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Farmer_Info GetOneFarmer(int FarmID)
        {
            Farmer_Info objFarmer_Info = new Farmer_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Farmer_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FarmID", FarmID));
            try
            {
                objConnection.Open();
                SqlDataReader ordFarmer = command.ExecuteReader(); //OjectRecordDatareader
                ordFarmer.Read();
                if (ordFarmer.HasRows)
                {
                    if (ordFarmer["FarmID"] != DBNull.Value)
                    {
                        objFarmer_Info.FarmID = Convert.ToInt16(ordFarmer["FarmID"]);
                    }
                    if (ordFarmer["MAFFLicenceID"] != DBNull.Value)
                    {
                        objFarmer_Info.MAFFLicenceID = Convert.ToString(ordFarmer["MAFFLicenceID"]);
                    }
                    if (ordFarmer["FarmerName"] != DBNull.Value)
                    {
                        objFarmer_Info.FarmerName = Convert.ToString(ordFarmer["FarmerName"]);
                    }
                    if (ordFarmer["FarmName"] != DBNull.Value)
                    {
                        objFarmer_Info.FarmName = Convert.ToString(ordFarmer["FarmName"]);
                    }
                    if (ordFarmer["Username"] != DBNull.Value)
                    {
                        objFarmer_Info.Username = Convert.ToString(ordFarmer["Username"]);
                    }
                    if (ordFarmer["Password"] != DBNull.Value)
                    {
                        objFarmer_Info.Password = Convert.ToString(ordFarmer["Password"]);
                    }
                    if (ordFarmer["Address"] != DBNull.Value)
                    {
                        objFarmer_Info.Address = Convert.ToString(ordFarmer["Address"]);
                    }
                    if (ordFarmer["PhoneNumber"] != DBNull.Value)
                    {
                        objFarmer_Info.PhoneNumber = Convert.ToString(ordFarmer["PhoneNumber"]);
                    }
                    if (ordFarmer["Email"] != DBNull.Value)
                    {
                        objFarmer_Info.Email = Convert.ToString(ordFarmer["Email"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objFarmer_Info;
        }
    }
}
